//
//  SRGradeListViewController.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/3/31.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRGradeListViewController.h"

#import "SRGradeItemModel.h"
#import "FZHomeCourseModel.h"
#import "FZHomeConstants.h"
#import "SRAlbumService.h"

#import "SRGradeItemCollectionViewCell.h"

#import "SRAlbumListViewController.h"


@interface SRGradeListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong, nonatomic) UICollectionView          *collectionView;
@property (strong, nonatomic) NSMutableArray            *items;

@end

@implementation SRGradeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"添加课程";
    
    [self setupData];
    [self setupView];

    
    WEAKSELF
    self.loadingView.clickButtonBlock = ^(id sender) {
        [weakSelf queryListData:NO];
    };
    
    [self showLoadingViewWithCompletedBlock:nil];
    [self queryListData:NO];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Setup


- (void)setupData {
    self.items = [NSMutableArray arrayWithCapacity:0];
}

- (void)setupView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    CGFloat edgeSpace = kFZHomeCollectionViewEdgeSpace;
    
    CGFloat cellWidth = floorf((SCREEN_WIDTH - edgeSpace * 3) / 2.f) - 1;
    CGFloat cellHeight = 55;
    
    layout.itemSize = CGSizeMake(cellWidth, cellHeight);
    layout.minimumLineSpacing = edgeSpace;
    layout.minimumInteritemSpacing = edgeSpace;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(edgeSpace * 2, edgeSpace, edgeSpace, edgeSpace);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.alwaysBounceVertical = YES;
    [collectionView registerNib:[UINib nibWithNibName:kSRGradeItemCollectionViewCell bundle:nil] forCellWithReuseIdentifier:kSRGradeItemCollectionViewCell];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    self.collectionView.backgroundColor = [FZStyleSheet currentStyleSheet].color_2;

    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.bottom.equalTo(self.view);
    }];
}

- (void)queryListData:(BOOL)isRefresh{
    WEAKSELF
    
    [SRAlbumService queryGradeListSuccessBlock:^(NSInteger statusCode, NSString *message, id responseDataObject) {
        if (statusCode == kFZRequestStatusCodeSuccess) {
            NSArray *addItems = responseDataObject;
            if ([addItems isKindOfClass:[NSArray class]]) {
                [weakSelf.items removeAllObjects];
                [weakSelf.items addObjectsFromArray:addItems];
                [weakSelf hideLoadingView];
                [weakSelf.collectionView reloadData];
                
            } else {
                [weakSelf emptyLoadingViewWithTitle:nil];
            }
            
        } else {
            [weakSelf failedLoadingViewWithTitle:message];
        }
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf failedLoadingViewWithTitle:nil];
    }];
}

#pragma mark - UICollection DataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.items count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SRGradeItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSRGradeItemCollectionViewCell forIndexPath:indexPath];
    if (indexPath.item > [self.items count]) {
        return cell;
    }
    SRGradeItemModel *item = self.items[indexPath.item];
    cell.gradeLabel.text = item.grade;
    
    return cell;
}

#pragma mark - UICollectionViewDelegate


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SRGradeItemModel *item = self.items[indexPath.item];
    
    SRAlbumListViewController *vc = [[SRAlbumListViewController alloc] init];
    vc.gradeModel = item;
    vc.isAddHomework = self.isAddHomework;
    vc.groupID = self.groupID;
    
    [self.navigationController pushViewController:vc animated:YES];
}




@end
